const delCookie=(name)=>{
  const exp = new Date();
  exp.setTime(exp.getTime() - (1*24*60*60*1000));
  const cval=getCookie(name);
  if (cval!=null) {
    document.cookie= name + "='';expires=";
    document.cookie= name + "='';expires="+exp.toGMTString();
  }
}

const getCookie=(name)=>{
  const reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
  let arr = document.cookie.match(reg)
  if(arr)
      return unescape(arr[2]);
  else
      return null;
}
//如果需要设定自定义过期时间
//那么把上面的setCookie　函数换成下面两个函数就ok;
//程序代码
const setCookie = (name,value,time)=>{
    const strsec = getsec(time);
    const exp = new Date();
    exp.setTime(exp.getTime() + strsec*1);
    if (getCookie(name)) {
      delCookie(name)
    }
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getsec(str)
{
    const str1=str.substring(1,str.length)*1;
    const str2=str.substring(0,1);
    if (str2==="s")
    {
        return str1*1000;
    }
    else if (str2==="h")
    {
        return str1*60*60*1000;
    }
    else if (str2==="d")
    {
        return str1*24*60*60*1000;
    }
}   
export {
  setCookie,
  getCookie,
  delCookie
}